﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataContextListenerTests.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the DataContextListenerTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Windows
{
    using System.Windows;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class DataContextListenerTests
    {
        public class TestElement : FrameworkElement
        {
            
        }

        private int ChangeCount { get; set; }

        public RoutedPropertyChangedEventHandler<object> PropertyChanged;

        public DataContextListenerTests()
        {
            PropertyChanged = (s, e) => ChangeCount++;
        }

        [TestMethod]
        public void When_Data_Context_Is_Set_Should_Notify()
        {
            var testElement = new TestElement();
            DataContextListener.AddHandler(testElement, PropertyChanged);
            ChangeCount = 0;
            testElement.DataContext = new object();
            Assert.AreEqual(1, ChangeCount);
        }
        
        [TestMethod]
        public void When_Data_Context_Is_Changed_Should_Notify()
        {
            var testElement = new TestElement();
            DataContextListener.AddHandler(testElement, PropertyChanged);
            ChangeCount = 0;
            testElement.DataContext = new object();
            Assert.AreEqual(1, ChangeCount);
            testElement.DataContext = new object();
            Assert.AreEqual(2, ChangeCount);
            testElement.DataContext = null;
            Assert.AreEqual(3, ChangeCount);
        }

        [TestMethod]
        public void When_Data_Context_Is_Cleared_Should_Notify()
        {
            var testElement = new TestElement();
            testElement.DataContext = new object();
            DataContextListener.AddHandler(testElement, PropertyChanged);
            ChangeCount = 0;
            testElement.ClearValue(FrameworkElement.DataContextProperty);
            Assert.AreEqual(1, ChangeCount);
        }
    }
}
